package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookControler {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R addDiZhi(@RequestBody AddressBook addressBook){
        log.info("添加地址:"+addressBook);
        addressBook.setUserId(UserHolder.getUserId());
        boolean save = addressBookService.save(addressBook);

        return save?R.success("addressBook"):R.error("!addressBook");
    }

    /**
     * 设置默认值
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R defaultMoRen(@RequestBody AddressBook addressBook){

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId, UserHolder.getUserId());
        qw.set(AddressBook::getIsDefault,0);
        addressBookService.update(qw);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R AllIdChaXun(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null){
            return R.success(addressBook);
        } else {
            return R.error("没有找到对象");
        }

    }

    /**
     * 默认地址查询
     * @return
     */
    @GetMapping("/default")
    public R defaultMorenshezhi(){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,UserHolder.getUserId());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
        if (one == null){
            return R.error("没有找到数据");
        } else {
            return R.success(one);
        }

    }

    /**
     * 查询全部
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R list(AddressBook addressBook){
        addressBook.setUserId(UserHolder.getUserId());

        //构造器  --条件
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(addressBook.getUserId() != null,AddressBook::getUserId,addressBook.getUserId());
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }



    @DeleteMapping
    public R deleteShanchu(@RequestParam List<Long> ids){
        addressBookService.deleteShanchu(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R updateDizhi(@RequestBody AddressBook addressBook, HttpServletRequest request){


        boolean update = addressBookService.updateById(addressBook);
        return update ? R.success("修改地址成功") : R.error("修改地址失败");

    }

}
